/*
 * nwpu/2011/2011_5.c
 *
 * Harry Wei <harryxiyou@gmail.com> (C) 2013
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define DENTRY		"D:\\test\\polyn.txt"

struct entry {
	int coe;
	int degree;
};

int poly_malloc(struct entry **poly, int nums) {
			*poly = (struct entry *) malloc(sizeof(struct entry) * nums);
			if (NULL == *poly) {
				fprintf(stderr, "%s\n", "Allocate error!");
				return -1;
			}
			return 0;
}

int calculate(struct entry *_poly, int _nums, 
			  struct entry *__poly, int __nums) {
	int nums = _nums + __nums;
	struct entry *poly = NULL;
	int i = 0;
	int j = 0;
	int ret = 0;
	int flag = 0;
	int temp = 0;

	for (i = 0; i < _nums; i++) {
		for (j = 0; j < __nums; j++) {
			if (_poly[i].degree == __poly[j].degree) {
				nums -= 1;
			}
		}
	}
	printf("999 nums is %d\n", nums);
	ret = poly_malloc(&poly, nums);
	if (0 > ret) {
		fprintf(stderr, "%s\n", "Allocate error!");
		return -1;
	}
	memcpy(poly, __poly, sizeof(struct entry) * __nums);
	temp = __nums;
	printf("temp is %d\n", temp);

	for (i = 0; i < _nums; i++) {
		for (j = 0; j < __nums; j++) {
			if ((_poly[i].degree == __poly[j].degree) && (0 == flag)) {
				poly[j].coe = poly[j].coe + _poly[i].coe;
				flag = 1;
			} else if ((0 == flag) && (j == (__nums - 1))){
//				printf("999 %d , %d\n", poly[i].coe, poly[i].degree);
				poly[temp].coe = _poly[i].coe;
				poly[temp].degree = _poly[i].degree;
				temp += 1;
				flag = 1;
			} else {
				continue;
			}
		}
		flag = 0;
	}

	for (i = 0; i < nums; i++) {
		printf("coe: %d, de: %d\n", poly[i].coe, poly[i].degree);
	}
	free(poly);

	return 0;
}

/* Input nums.
 * 3 3 5 -2 1 4 0
 * 4 2 3 -1 2 1 1 3 0
 * 0
 * 0
 */

int main(int argc, char *argv[]) {
	/*TODO: Reduce the variable nums*/
	char ch = 0;
	int flag = 0;
	int end_flag = 0;
	int coe_de_flag = 0;
	int ret = 0;
	FILE *fp = NULL;
	struct entry *poly = NULL;
	struct entry *_poly = NULL;
	struct entry *__poly = NULL;
	int nums = 0;
	int _nums = 0;
	int fnums = 0;
	int snums = 0;
	int i = 0;
	int is_negtive = 0;
	int cal_flag = 0;

	fp = fopen(DENTRY, "rt");
	if (NULL == fp) {
		fprintf(stderr, "%s\n", "Fopen file error!");
		return -1;
	}
	while (!feof(fp)) {
		fscanf(fp, "%c", &ch);
		if (('0' == ch) && (flag == 0)) {
			/*TODO: free jobs*/
			goto out;
		}
//		printf("%c", ch);
		/*TODO: degree beyond 0-9*/
		if (0 == flag) {
			/*Init flag nums*/
			coe_de_flag = 0;
			is_negtive = 0;
			_nums = 0;
			flag = 1;
			nums = atoi(&ch);
			printf("999 nums: %d\n", nums);
			ret = poly_malloc(&poly, nums);
			if (0 > ret) {
				fprintf(stderr, "%s\n", "Allocate error!");
				goto out;
			}
			continue;
		}
		if ('-' == ch) {
			is_negtive = 1;
		}
		if (('0' <= ch && ch <= '9') && (1 == flag)) {
			printf("999 ch: %c\n", ch);
			if (0 == coe_de_flag) {
				poly[_nums].coe = atoi(&ch);
				if (1 == is_negtive) {
					is_negtive = 0;
					poly[_nums].coe = -poly[_nums].coe;
				}
				coe_de_flag = 1;
			} else {
				poly[_nums].degree = atoi(&ch);
				if (1 == is_negtive) {
					is_negtive = 0;
					poly[_nums].degree = -poly[_nums].degree;
				}
				coe_de_flag = 0;
				_nums += 1;
			}
		}
		if ('\n' == ch) {
			flag = 0;
			for (i = 0; i < nums; i++) {
				printf("coe: %d, de: %d\n", poly[i].coe, poly[i].degree);
			}
			if (0 == cal_flag) {
				fnums = nums;
				ret = poly_malloc(&_poly, nums);
				if (0 > ret) {
					fprintf(stderr, "%s\n", "Allocate error!");
					goto out;
				}
				memcpy(_poly, poly, sizeof(struct entry) * nums);
				free(poly);
				printf("999 after free ;-D\n");
				for (i = 0; i < nums; i++) {
					printf("coe: %d, de: %d\n", _poly[i].coe, _poly[i].degree);
				}
				cal_flag = 1;
			} else {
				cal_flag = 0;
				snums = nums;
				ret = poly_malloc(&__poly, nums);
				if (0 > ret) {
					fprintf(stderr, "%s\n", "Allocate error!");
					goto out;
				}
				memcpy(__poly, poly, sizeof(struct entry) * nums);
				free(poly);
				printf("999 after free ;-D\n");
				for (i = 0; i < nums; i++) {
					printf("coe: %d, de: %d\n", __poly[i].coe, __poly[i].degree);
				}
				//break;
				calculate(_poly, fnums, __poly, snums);
				free(_poly);
				free(__poly);
			}
		}
	}
	printf("\n");
out:
	fclose(fp);
	return ret;
}